package smedi.ismedi.web.bff.controller.labor;

import com.smedi.ismedi.labor.api.dto.response.IdCardFrontOcrResultResponse;
import com.smedi.ismedi.labor.api.dto.response.ProfileImageOcrResultResponse;
import com.smedi.ismedi.labor.api.dto.response.RemoteLaborProfileResponse;
import com.smedi.ismedi.labor.api.feign.RemoteLaborAssistFeignApi;
import com.smedi.ismedi.web.starter.common.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import smedi.ismedi.web.bff.controller.BaseController;
import smedi.ismedi.web.bff.infrastructure.util.FileHelper;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * 劳务人员文件管理接口
 *
 * @author xulinjun
 */
@Slf4j
@RestController
@Api(tags = {"劳务人员文件管理接口"})
@RequestMapping("/webbff/v1/laborAssist")
@RequiredArgsConstructor
public class LaborAssistController extends BaseController {
    private final RemoteLaborAssistFeignApi remoteLaborAssistFeignApi;

    @Value("${fdfs.service.address.in}")
    private String FDFS_SERVICE_ADDRESS_IN;

    @Value("${fdfs.service.address.out}")
    private String FDFS_SERVICE_ADDRESS_OUT;


    /**
     * 下载文件输出
     *
     * @param httpServletResponse
     * @param fileName
     * @param bytes
     * @throws IOException
     */
    private void writeHttpServletResponse(HttpServletResponse httpServletResponse, String fileName, byte[] bytes) throws IOException {
        httpServletResponse.reset();
        httpServletResponse.setContentType("application/x-download");
        httpServletResponse.addHeader("Content-Disposition", "attachment;filename=\"" + URLEncoder.encode(fileName, "UTF-8") + "\"");
        httpServletResponse.getOutputStream().write(bytes);
        httpServletResponse.getOutputStream().close();
    }

    /**
     * 上传身份证正面
     *
     * @param file
     * @return
     */
    @ApiOperation(httpMethod = "POST", value = "上传身份证正面")
    @PostMapping(value = "/uploadIdCardFront", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseData<IdCardFrontOcrResultResponse> postUploadIdCardFront(@RequestPart("file") MultipartFile file) {
        return remoteLaborAssistFeignApi.postUploadIdCardFront(file);
    }

    /**
     * 上传身份证背面
     *
     * @param file
     * @return
     */
    @ApiOperation(httpMethod = "POST", value = "上传身份证背面")
    @PostMapping(value = "/uploadIdCardBack", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseData<String> postUploadIdCardBack(@RequestPart("file") MultipartFile file) {
        return remoteLaborAssistFeignApi.postUploadIdCardBack(file);
    }

    /**
     * 上传人员头像照片
     */
    @ApiOperation(httpMethod = "POST", value = "上传人员头像照片")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "idCardFrontOcrId", value = "身份证正面识别id", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(paramType = "query", name = "containBase64", value = "返回是否包含了人像分割后的base64", dataType = "boolean", dataTypeClass = boolean.class)
    })
    @PostMapping(value = "/uploadProfileImage", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseData<ProfileImageOcrResultResponse> postUploadProfileImage(@RequestPart("file") MultipartFile file,
                                                                              @RequestParam("idCardFrontOcrId") String idCardFrontOcrId,
                                                                              @RequestParam(value = "containBase64", required = false, defaultValue = "false") boolean containBase64) {
        return remoteLaborAssistFeignApi.postUploadProfileImage(file, idCardFrontOcrId, containBase64);
    }

    /**
     * 上传三级安全教育证书
     *
     * @param file
     * @return
     */
    @ApiOperation(httpMethod = "POST", value = "上传三级安全教育证书")
    @PostMapping(value = "/uploadCertificationNotSave", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseData<String> postUploadCertificationNotSave(@RequestPart("file") MultipartFile file) {
        return remoteLaborAssistFeignApi.postUploadCertificationNotSave(file);
    }

    /**
     * 下载人员基本信息附件（身份证、照片）
     *
     * @param fileKey
     */
    @ApiOperation(httpMethod = "GET", value = "下载人员基本信息附件（身份证、照片）")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "fileKey", value = "文件关键id", dataType = "String", dataTypeClass = String.class)
    })
    @GetMapping(value = "/downloadLaborProfile")
    public void getDownloadLaborProfile(@RequestParam("fileKey") String fileKey, HttpServletResponse httpServletResponse) {
        RemoteLaborProfileResponse fileInfoResponse = remoteLaborAssistFeignApi.getDownloadLaborProfile(fileKey).getData();
        FileHelper.downloadFile(FDFS_SERVICE_ADDRESS_IN, fileInfoResponse.getFileGroup(), fileInfoResponse.getFilePath(), fileInfoResponse.getFileName(), httpServletResponse);
    }

    /**
     * 下载人员三级安全教育证书附件
     *
     * @param fileKey
     */
    @ApiOperation(httpMethod = "GET", value = "下载人员三级安全教育证书附件")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "fileKey", value = "文件关键id", dataType = "String", dataTypeClass = String.class)
    })
    @GetMapping(value = "/downloadLaborCertificationFile")
    public void getDownloadLaborCertificationFile(@RequestParam("fileKey") String fileKey, HttpServletResponse httpServletResponse) {
        RemoteLaborProfileResponse fileInfoResponse = remoteLaborAssistFeignApi.getDownloadLaborCertificationFile(fileKey).getData();
        FileHelper.downloadFile(FDFS_SERVICE_ADDRESS_IN, fileInfoResponse.getFileGroup(), fileInfoResponse.getFilePath(), fileInfoResponse.getFileName(), httpServletResponse);
    }
}
